home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-src-22.lha / AmiTCP-2.2 / src / l / inet-handler / handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  3.7 KB  |  161 lines

  1. /*
  2.  * handler.c
  3.  *
  4.  * Author: Tomi Ollila <too@cs.hut.fi>
  5.  *
  6.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  7.  *              All rights reserved.
  8.  *
  9.  * Created: Wed Sep  1 18:05:18 1993 too
  10.  * Last modified: Tue Nov 16 21:35:04 1993 too
  11.  *
  12.  * $Id: handler.c,v 1.2 1993/11/17 12:06:50 too Exp $
  13.  *
  14.  */
  15.  
  16. #if 0 && (__GNUC__ < 2) && (__SASC < 6) || !defined (AMIGA)
  17. #error this program compiles only with amiga, gcc v2.x or sas/c version 6.x
  18. #endif
  19.  
  20. /*
  21.  * These Globals are constant after initialization
  22.  */
  23. #ifdef __GNUC__
  24. struct ExecBase * SysBase = 0;
  25. #endif
  26. struct DosLibrary * DOSBase = 0;
  27.  
  28. #include "handler/begin.c"
  29. #define RFI .
  30.  
  31. #ifdef __SASC
  32. #undef SocketBase
  33. struct Library * SocketBase;
  34. #endif
  35.  
  36. int start()
  37. {
  38.   struct CleanUp    CU /* = { 0 } */;
  39.   struct ApplShared    AS;
  40.   struct ApplPort *    applarray[FD_SETSIZE];
  41.   struct MinList    writelist;    /* list of apps having pending wrts */
  42.   struct MinList    freewritelist;    /* free pending write structures */
  43.   struct MinList    freeappllist;
  44.   struct MsgPort *    mymsgport;
  45.   ULONG         myportflag;
  46.   ULONG         timerflag;
  47.   struct DosList *    mydev;
  48.   int            nfds;
  49.   fd_set        readfds, writefds;
  50.   ULONG         sigmask; /* Exec signals to be catched */
  51.   int            writespending = 0; /* counter */
  52.   struct ApplPort *    lastappl = NULL;
  53.   LONG            lastmask = 0;
  54.   int            lastsd = 0;
  55.   int            notDone = TRUE;
  56.  
  57. #include "handler/startup.c"
  58.  
  59.   while (notDone) {
  60.     fd_set        rfds;
  61.     fd_set        wfds;
  62.     fd_set *        wfdsp;
  63.     ULONG        smask;
  64.     int            n;
  65.     
  66.     rfds = readfds;
  67.     if (writespending) {
  68.       DP("writefds %lx", *(LONG *)&writefds);
  69.       wfds = writefds;
  70.       wfdsp = &wfds;
  71.     }
  72.     else
  73.       wfdsp = NULL;
  74.     smask = sigmask;
  75.  
  76.     /*
  77.      * All Input events are waited here.
  78.      */
  79. /*    switch(WaitSelect(nfds, &rfds, wfdsp, NULL, NULL, &smask)) { */
  80. /*    switch (WaitSelect2(nfds, &rfds, wfdsp, &smask)) { */
  81.  
  82.     n = (WaitSelect2(nfds, &rfds, wfdsp, &smask));
  83.     DP("n %ld, wfds %lx smask %lx", n,
  84.     writespending? *(LONG *)wfdsp: 0, smask);
  85.  
  86.     switch (n) {
  87.     case -1:
  88.       notDone = FALSE;
  89.       break;
  90.     case 0:
  91.       /*
  92.        * Since there is no timeout used the only possibility is there
  93.        * perhaps is packet arrived. since the case where something has
  94.        * arrived to sockets there is a possibisity a packet has arrived
  95.        * -- the packet handling is done last under 'default' case.
  96.        */
  97.       goto packet;
  98.     default:
  99.       if (writespending) {
  100.     DP("wfds %lx", *(LONG *)wfdsp);
  101. #    include "handler/pendingwrites.c"
  102.       }
  103.       /*
  104.        * First look if data has arrived to the socket where it last arrived.
  105.        * this speeds up net burps. If not, scan whole application array
  106.        * through
  107.        */
  108.       if (*(LONG *)&rfds) {
  109.     int i;
  110.  
  111.     DP("lastmask %ld, rfds %ld\n", lastmask, *(LONG *)&rfds);
  112.  
  113.     if (lastmask == *(LONG*)&rfds) {
  114.       i = nfds;
  115.       goto readit;
  116.     }
  117.     i = 0;
  118.       loop:
  119.     if (FD_ISSET(i, &rfds)) {
  120.       lastappl = applarray[i];
  121.       lastsd = i;
  122.     readit:
  123. #      include "handler/readevent.c"
  124.     }
  125.     i++;
  126.     if (i < nfds)
  127.       goto loop;
  128.     lastmask = 1 << lastsd;
  129.       }
  130.     packet:
  131.       /*
  132.        * First see if one or more of the applications have sent packet
  133.        * to their private port (other than an open/reopen -packet).
  134.        */
  135.       if (smask & applportflag) {
  136. #    include "handler/applmsg.c"
  137.       }
  138.       /*
  139.        * See if some of the waitforchar requests are expired
  140.        */
  141.       if (smask & timerflag) {
  142. #    include "handler/timermsg.c"
  143.       }
  144.       /*
  145.        * See if someone has sent message to my message port.
  146.        */
  147.  
  148.       if (smask & myportflag) {
  149. #    include "handler/mymsg.c"
  150.       }
  151.     } /* WaitSelect() */
  152.   }  /* while */
  153.  
  154.   /*
  155.    * Program received break signal. drop out all connections.
  156.    */
  157. # include "handler/cleanup.c"
  158.  
  159.   return clean(&CU, 0);
  160. }
  161.